﻿using System;
using System.Collections.Generic;
using Accountant.DataModel;
using Accountant.Utils;

namespace Accountant.Primitives
{
  public class GroupTransaction
  {
    public List<Transaction> Transactions { get; private set; }

    public string Comment
    {
      get { return Transactions[0].Comment; }
      set { Transactions[0].Comment = value; }
    }
    public DateTime Timestamp
    {
      get { return Transactions[0].Timestamp; }
      set { Transactions[0].Timestamp = value; }
    }

    public GroupTransaction(List<Transaction> transactions)
    {
      if (transactions == null) throw new ArgumentNullException("transactions");
      if (transactions.Count < 2) throw new ArgumentException("Group transaction must not consist of less than two transactions");
      Transactions = transactions;
    }

    public static GroupTransaction Create(List<Transaction> transactions)
    {
      if (transactions.Count < 2) return null;
      if (transactions.Count == 2)
        if (transactions[0].Credit.IsExchange())
          if (transactions[1].Debit.IsExchange())
            return new ExchangeTransaction(transactions);
      return new GroupTransaction(transactions);
    }
  }
}
